//
//  MainViewController.m
//  MacDevTest
//
//  Created by 夏利兵 on 2020/8/5.
//  Copyright © 2020 com.lbx. All rights reserved.
//

#import "MainViewController.h"
#import "NewWindowController.h"
#import "AppDelegate.h"
#import "CustomNSView.h"
#import "CustomModelAlert.h"
#import "NormalTableView.h"
#import "FriendListWindowController.h"
#import "QQMainChatWindow.h"
#import "QQLogInWindowController.h"
#import "QQWindowManage.h"
#import "TestCollectionViewWindowController.h"
#import "TestUI.h"

@interface MainViewController ()
@property (nonatomic, strong) NewWindowController *nextWindow;
@property (nonatomic, strong) NormalTableView *tableViewWindow;
@property (nonatomic, strong) FriendListWindowController *friendWinow;
//@property (nonatomic, strong) QQMainChatWindow *qqMainWindow;
//@property (nonatomic, strong) QQLogInWindowController *qqLogInWindow;

@property (nonatomic, strong) TestCollectionViewWindowController *testCollectionViewWindow;
@end

@implementation MainViewController

//- (void)loadView
//{
//    self.view = [[CustomNSView alloc]init];
//}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do view setup here.
    
    //xib中设置view类为 CustomNSView
    
    //NSView设置背景色的2中方式
    if ([self.view isKindOfClass:[CustomNSView class]]) {
        CustomNSView *view = (CustomNSView*)self.view;
        view.onDrawRect = ^(NSRect dirtyRect) {
             [[NSColor yellowColor] setFill];
                NSRectFill(dirtyRect);
        };
    }
    
//    NSView *view = [[NSView alloc]init];
//    view.frame = NSMakeRect(0, 100, 100, 100);
//    view.wantsLayer = YES;
//    view.layer.backgroundColor = [NSColor redColor].CGColor;
//    [self.view addSubview:view];
    
    [self addEvent];
}

-(void)addEvent
{
   NSEvent *event = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown handler:^NSEvent * _Nullable(NSEvent * _Nonnull aEvent) {
        [self keyDown:aEvent];
        return aEvent;
    }];
    
    //取消监控
    [NSEvent removeMonitor:event];
    
   event = [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskFlagsChanged handler:^NSEvent * _Nullable(NSEvent * _Nonnull aEvent) {
        [self flagsChanged:aEvent];
        return aEvent;
    }];
    
    //取消监控
       [NSEvent removeMonitor:event];
    
    
    /* NSWindow Notifications */
//    APPKIT_EXTERN NSNotificationName NSWindowDidBecomeKeyNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowDidBecomeMainNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowDidChangeScreenNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowDidDeminiaturizeNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowDidExposeNotification;            // userInfo key:  @"NSExposedRect"
//    APPKIT_EXTERN NSNotificationName NSWindowDidMiniaturizeNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowDidMoveNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowDidResignKeyNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowDidResignMainNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowDidResizeNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowDidUpdateNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowWillCloseNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowWillMiniaturizeNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowWillMoveNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowWillBeginSheetNotification;
//    APPKIT_EXTERN NSNotificationName NSWindowDidEndSheetNotification;
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(handNotification:) name:NSWindowDidResizeNotification object:nil];
    
}

- (void)handNotification:(NSNotification*)notification
{
    NSLog(@"notification");
}

-(void)keyDown:(NSEvent *)event{
    NSLog(@"%hu",event.keyCode);
}
 

- (void)flagsChanged:(NSEvent *)event
{
     NSLog(@"%hu",event.keyCode);
}


- (IBAction)newWindow:(id)sender {
    
    if (_nextWindow) {
        
        [_nextWindow showWindow:nil];
        return;
    }
    
    //弹出新窗口, NS
    NewWindowController *newWindow = [[NewWindowController alloc]initWithWindowNibName:@"NewWindowController"];
    
    
    //下面2行代码，均可以将新页面显示出来
    [newWindow.window orderFront:nil];
//    [newWindow showWindow:nil];
    
     //自己隐藏代码，不调用下面代码，老窗口和新窗口同时显示
//    AppDelegate *delegate =(AppDelegate*) [NSApplication sharedApplication].delegate;
//    [delegate.window orderOut:nil];
    //下面获取的keyWindow也是一样
//    [[NSApplication sharedApplication].keyWindow orderOut:nil];
    
    //不保存也可以,但是有时候WindowController内设置的其他控件的代理会提示对象已经销毁
    self.nextWindow  = newWindow;
}
- (IBAction)testAlert:(id)sender {
    
    CustomModelAlert *alert = [[CustomModelAlert alloc]initWithWindowNibName:@"CustomModelAlert"];
    
    [alert showWindow:nil];
}
- (IBAction)normalTableView:(id)sender {
    
    if (_tableViewWindow) {
        [_tableViewWindow showWindow:nil];
        return;
    }
    
    NormalTableView *tableWindow = [[NormalTableView alloc]initWithWindowNibName:@"NormalTableView"];
    
    [tableWindow showWindow:nil];
    
    self.tableViewWindow =  tableWindow;
}

- (IBAction)macQQ:(id)sender {
    
    QQLogInWindowController *qqLogInWindow = [[QQLogInWindowController alloc]initWithWindowNibName:@"QQLogInWindowController"];
    
    [qqLogInWindow showWindow:nil];
    
    [QQWindowManage sharedManager].qqLogWindow = qqLogInWindow;
    //keywindow
    [qqLogInWindow.window makeKeyAndOrderFront:nil];
    
    //一开始的keywindow消失
    AppDelegate *delegte = [NSApplication sharedApplication].delegate;
    [delegte.window close];
}
- (IBAction)testCollectionView:(id)sender {
    
    self.testCollectionViewWindow = [[TestCollectionViewWindowController alloc]initWithWindowNibName:@"TestCollectionViewWindowController"];
    
    [self.testCollectionViewWindow showWindow:nil];
}

@end
